#include "CCEditBox.h"
using namespace cocos2d;

#include "LoginState.h"
#include "DebugState.h"
#include "GameState.h"

CCScene* LoginState::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    LoginState *layer = LoginState::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool LoginState::init()
{
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    COCOUISYSTEM->resetSystem(this);    
    COCOUISYSTEM->getCurScene()->addWidget(COCOUISYSTEM->createWidgetFromFileWithAdapt_json("UILogin.ExportJson", true, true));
    
    cs::CocoTextButton* bt = (cs::CocoTextButton*)(COCOUISYSTEM->getWidgetByName("btnLogin"));
    bt->addReleaseEvent(this, coco_releaseselector(LoginState::menuLoginCallback));

	bt = (cs::CocoTextButton*)(COCOUISYSTEM->getWidgetByName("btnDebug"));
	bt->addReleaseEvent(this, coco_releaseselector(LoginState::menuDebugCallback));

	bt = (cs::CocoTextButton*)(COCOUISYSTEM->getWidgetByName("btnSelServer"));
	bt->addReleaseEvent(this, coco_releaseselector(LoginState::menuSelectServerCallback));

    cs::CocoTextField* tfd = dynamic_cast<cs::CocoTextField*>(COCOUISYSTEM->getWidgetByName("tfUsername"));
    tfd->setCharacterLength(4);
    tfd->setIsPassWord(false);    
    
    return true;
}

void LoginState::menuSelectServerCallback( CCObject* pSender )
{
	//
	
}

void LoginState::menuLoginCallback( CCObject* pSender )
{
	//CCFadeOut* fo = CCFadeOut::create(0.5f);
	//CCSequence* seq = CCSequence::create(fo,NULL);
	//COCOUISYSTEM->getCurScene()->getRootWidget()->runAction(seq);

	CCScene *gameScene = GameState::scene();
	CCDirector::sharedDirector()->pushScene(gameScene);
}

void LoginState::menuDebugCallback( CCObject* pSender )
{
	CCScene *gameOverScene = DebugState::scene();
	//gameOverScene->getLayer()->getLabel()->setString("You Lose :[");
	CCDirector::sharedDirector()->pushScene(gameOverScene);
}

